package com.example.navigation.components

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.Button
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.DateRangePicker
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.rememberDateRangePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import kotlinx.coroutines.currentCoroutineContext
import java.text.SimpleDateFormat
import java.util.Locale
import kotlin.coroutines.coroutineContext

/**
 * Created with IntelliJ IDEA.
 * @Author: Liao Run
 * @Date: 2024-06-10 0:57
 * @Description:
 **/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyDateRangePicker(context: Context, search: (beDate: String, endDate: String) -> Unit) {
    var dateRange by remember {
        mutableStateOf("")
    }
    var isShowDialog by remember {
        mutableStateOf(false)
    }

    var canEditDateRange by remember {
        mutableStateOf(false)
    }

    val dateFormat by remember {
        mutableStateOf(
            SimpleDateFormat("yyyy-MM-dd 00:mm:ss", Locale.CHINA)
        )
    }
    TextField(
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
        keyboardActions = KeyboardActions(onSearch = {
            // 确定输入的是正确的时间
            val dates = dateRange.split(" 至 ")
            try {
                dateFormat.parse(dates[0])
                dateFormat.parse(dates[1])
                // 正确的时间
                search(dates[0], dates[1])
            } catch (e: Exception) {
                Toast.makeText(context, "你输入的不是正确的时间格式", Toast.LENGTH_SHORT).show()
            }
        }),
        suffix = {
            IconButton(onClick = {
                canEditDateRange = !canEditDateRange
            }) {
                Icon(imageVector = Icons.Default.Edit, contentDescription = "编辑")
            }
        },
        value = dateRange,
        onValueChange = {
            dateRange = it
        },
        enabled = canEditDateRange,
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                isShowDialog = true
            },
        placeholder = {
            Text(text = "点击选择时间段或者点击编辑按钮手动输入")
        }
    )


    val pickerState = rememberDateRangePickerState()

    if (isShowDialog) {
        DatePickerDialog(
            onDismissRequest = { isShowDialog = false },
            confirmButton = {
                Button(onClick = {
                    try {
                        // 显示选择的时间段
                        dateRange =
                            dateFormat.format(pickerState.selectedStartDateMillis) + " 至 " +
                                    dateFormat.format(pickerState.selectedEndDateMillis)
                        isShowDialog = false
                        // 通知父组件我选择了时间
                        search(
                            dateFormat.format(pickerState.selectedStartDateMillis),
                            dateFormat.format(pickerState.selectedEndDateMillis)
                        )
                    } catch (e: Exception) {
                        Log.e("tag", "MyDateRangePicker: $e")
                        Toast.makeText(context, "时间格式不正确", Toast.LENGTH_SHORT).show()
                    }
                }) {
                    Text(text = "确定")
                }
            }) {
            DateRangePicker(
                state = pickerState,
                headline = {
                    Text(text = "选择查询时间范围")
                })
        }
    }
}